﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Presentation;
using Infrastructure;
using Domain;

namespace WinClient
{
    public partial class ConverterForm : Form, IView, ILog
    {
        private readonly Presenter presenter;
        private readonly LogText logText;
        private readonly LogHtml logHtml;

        public event Action OnConvertClick;
        public event Action OnSaveQuotationsClick;
        public event Action OnAddCurrencyClick;
        public event Action<ILog, bool> OnLogCheckedChanged;

        public ConverterForm()
        {
            InitializeComponent();
            presenter = new Presenter(this);
            logText = new LogText();
            logHtml = new LogHtml();
            presenter.AddLogger(logText);
            presenter.AddLogger(logHtml);
            presenter.AddLogger(this);
        }

        public string FromName 
        {
            get { return fromList.Text; } 
        }

        public string FromValue 
        {
            get { return fromValue.Text; }
        }

        public string ToName 
        {
            get { return toList.Text; }
        }

        public string ToValue 
        {
            set { toValue.Text = value; }
        }

        public string[] Currencies
        {
            get
            {
                var arr = new object[fromList.Items.Count];
                fromList.Items.CopyTo(arr, 0);
                return (string[]) arr;
            }

            set
            {
                toList.Items.Clear();
                fromList.Items.Clear();

                toList.Items.AddRange(value);
                fromList.Items.AddRange(value);
            }
        }

        public Dictionary<string, string> Quotations 
        { 
            get
            {
                var res = new Dictionary<string, string>();
                for (var i = 0; i < quotationsList.RowCount; i++)
                {
                    try
                    {
                        res.Add(quotationsList.Rows[i].Cells[0].Value.ToString(), quotationsList.Rows[i].Cells[1].Value.ToString());
                    }
                    catch
                    {
                        res.Add(quotationsList.Rows[i].Cells[0].Value.ToString(), "not_a_number");
                    }
                }
                return res;
            }
            set
            {
                quotationsList.Rows.Clear();
                quotationsList.RowCount = value.Count;

                int i = 0;
                foreach (var pair in value)
                {
                    quotationsList.Rows[i].Cells[0].Value = pair.Key;
                    quotationsList.Rows[i].Cells[1].Value = pair.Value;
                    i++;
                }
            }
        }

        public string NewName 
        {
            get { return newName.Text; }
        }

        public string NewValue 
        {
            get { return newValue.Text; }
        }

        public string ErrorMessage 
        {
            set { errorMessage.Text = value; }
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            OnConvertClick();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            OnSaveQuotationsClick();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            OnAddCurrencyClick();
        }

        public void ClearError()
        {
            errorMessage.Text = "";
        }

        public string Log
        {
            get
            {
                return logBox.Text;
            }
            set
            {
                logBox.Text += value + Environment.NewLine;
            }
        }

        private void textLog_CheckedChanged(object sender, EventArgs e)
        {
            OnLogCheckedChanged(logText, textLog.Checked);
        }

        private void htmlLog_CheckedChanged(object sender, EventArgs e)
        {
            OnLogCheckedChanged(logHtml, htmlLog.Checked);
        }
    }
}
